<template>
    <div>
        <!--面包屑导航区-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图-->
        <el-card>
            <!--提示区域-->
            <el-alert
                    title="添加商品信息"
                    type="info"
                    show-icon
                    :closable="false">
            </el-alert>

            <!--步骤条-->
            <el-steps :active="activeIndex" finish-status="success" align-center>
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="创建完成"></el-step>
            </el-steps>
            <el-form label-position="top" label-width="80px" ref="baseInfoRef" :rules="rules"
                     :model="baseInfo">
                <el-tabs tab-position="left" style="height: 100%;" value="baseInfo"
                         :before-leave="beforeTabLeave" @tab-click="tabClick">
                    <el-tab-pane name="baseInfo" label="基本信息">
                        <el-form-item label="商品名称" prop="name">
                            <el-input v-model="baseInfo.name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="price">
                            <el-input v-model="baseInfo.price"></el-input>
                        </el-form-item>
                        <el-form-item label="商品重量" prop="weight">
                            <el-input v-model="baseInfo.weight"></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量" prop="qty">
                            <el-input v-model="baseInfo.qty"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类" prop="sort">
                            <el-cascader
                                    ref="cascaderRef"
                                    v-model="baseInfo.sort"
                                    :options="sortList"
                                    :props="cascaderProps"
                                    @change="handleChange"
                                    clearable>
                            </el-cascader>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name="param" label="商品参数">
                        <!--商品参数-->
                        <el-form-item :label="item.attrName" v-for="item in gParam" :key="item.id">
                            <el-checkbox-group v-model="item.vals">
                                <el-checkbox v-for="item1 in item.options"
                                             :label="item1.name"
                                             :key="item1.id" border></el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>

                    </el-tab-pane>
                    <el-tab-pane name="attr" label="商品属性">
                        <!--商品属性-->
                        <el-form-item :label="item.name" v-for="item in gAttr" :key="item.id">
                            <el-input v-model="item.value"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name="img" label="商品图片">
                        <!--action: 上传图片地址-->
                        <el-upload
                                action="https://jsonplaceholder.typicode.com/posts/"
                                :on-preview="handlePreview"
                                :on-remove="handleRemove"
                                :file-list="imgList"
                                list-type="picture"
                                :headers="headers"
                                :on-success="handleSuccess">
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane name="content" label="商品内容">
                        <quill-editor
                                ref="myQuillEditor"
                                v-model="baseInfo.content"/>
                        <el-button class="btnAdd" type="primary" @click="createGoods">创建完成</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>

        <!--图片预览-->
        <el-dialog
                title="图片预览"
                :visible.sync="previeDialogVisible"
                width="50%">
            <img class="previewImg" :src="previewImgPath" alt="预览图片">
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "AddGoods",
        data() {
            const checkPrice = (rule, value, callback) => {
                const regPrice = /^[1-9]{1}[0-9]*$|^0{1}\.{1}[0-9]+$|^[1-9]{1}[0-9]*\.{1}[0-9]+$/;
                if (!regPrice.test(value)) {
                    console.log(value);
                    callback(new Error('请输入合法的价格'));
                } else {
                    return callback();
                }
            };
            const checkWeight = (rule, value, callback) => {
                const regWeight = /^[1-9]{1}[0-9]*$|^0{1}\.{1}[0-9]+$|^[1-9]{1}[0-9]*\.{1}[0-9]+$/;
                if (!regWeight.test(value)) {
                    callback(new Error('请输入合法的重量'));
                } else {
                    return callback();
                }
            };
            const checkQty = (rule, value, callback) => {
                const regQty = /^\+?[1-9]\d*$/;
                if (!regQty.test(value)) {
                    callback(new Error('请输入合法的数量'));
                } else {
                    return callback();
                }
            };
            return {
                baseInfo: {
                    name: '',
                    price: '',
                    weight: '',
                    qty: '',
                    sort: ["zhinan", "daohang", "cexiangdaohang"],
                    pics: [],
                    content: ''
                },
                gParam: [
                    {
                        id: 1,
                        attrName: '版式',
                        // 这个属性需要自己封装进去
                        vals: ['圆的', '方的'],
                        options: [
                            {
                                id: 11,
                                name: '圆的',
                                checked: true
                            },
                            {
                                id: 12,
                                name: '扁的',
                                checked: true
                            }, {
                                id: 13,
                                name: '方的',
                                checked: true
                            },
                        ]
                    }, {
                        id: 2,
                        attrName: '颜色',
                        vals: ['白色'],
                        options: [
                            {
                                id: 21,
                                name: '白色',
                                checked: true
                            },
                            {
                                id: 22,
                                name: '紫色',
                                checked: true
                            }, {
                                id: 23,
                                name: '黑色',
                                checked: true
                            },
                        ]
                    },
                ],
                gAttr: [
                    {
                        id: 1,
                        name: '主体-商品名称',
                        value: '长虹电视'
                    }, {
                        id: 2,
                        name: '显示-曲面',
                        value: '是'
                    }, {
                        id: 3,
                        name: '系统-智能电视',
                        value: '智能电视'
                    }, {
                        id: 4,
                        name: '外观-壁挂安装',
                        value: '支持'
                    }, {
                        id: 5,
                        name: '价格(元)',
                        value: '5888'
                    }, {
                        id: 6,
                        name: '重量(千克)',
                        value: '12'
                    }, {
                        id: 7,
                        name: '长度(com)',
                        value: '170'
                    }, {
                        id: 8,
                        name: '宽度(com)',
                        value: '500'
                    }, {
                        id: 9,
                        name: '厚度(com)',
                        value: '15'
                    },
                ],
                imgList: [{
                    name: 'food.jpeg',
                    url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
                }, {
                    name: 'food2.jpeg',
                    url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
                }],
                // 设置上传请求头携带token
                headers: {Authorization: window.sessionStorage.getItem('token')},
                rules: {
                    name: [
                        {required: true, message: '请输入商品名称', trigger: 'blur'},
                        {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
                    ],
                    price: [
                        {required: true, message: '请输入商品价格', trigger: 'blur'},
                        {validator: checkPrice, trigger: 'blur'}
                    ],
                    weight: [
                        {required: true, message: '请输入商品重量', trigger: 'blur'},
                        {validator: checkWeight, trigger: 'blur'}
                    ],
                    qty: [
                        {required: true, message: '请输入商品数量', trigger: 'blur'},
                        {validator: checkQty, trigger: 'blur'}
                    ],
                    sort: [
                        {required: true, message: '请输入选择商品分类', trigger: 'blur'},
                        {validator: checkQty, trigger: 'blur'}
                    ],
                },
                tabs: ['baseInfo', 'param', 'attr', 'img', 'content', 'finish'],
                activeIndex: 0,
                //父级分类列表
                sortList: [
                    {
                        value: 'zhinan',
                        label: '指南',
                        children: [{
                            value: 'shejiyuanze',
                            label: '设计原则',
                            children: [{
                                value: 'yizhi',
                                label: '一致'
                            }, {
                                value: 'fankui',
                                label: '反馈'
                            }, {
                                value: 'xiaolv',
                                label: '效率'
                            }, {
                                value: 'kekong',
                                label: '可控'
                            }]
                        }, {
                            value: 'daohang',
                            label: '导航',
                            children: [{
                                value: 'cexiangdaohang',
                                label: '侧向导航'
                            }, {
                                value: 'dingbudaohang',
                                label: '顶部导航'
                            }]
                        }]
                    }, {
                        value: 'zujian',
                        label: '组件',
                        children: [{
                            value: 'basic',
                            label: 'Basic',
                            children: [{
                                value: 'layout',
                                label: 'Layout 布局'
                            }, {
                                value: 'color',
                                label: 'Color 色彩'
                            }, {
                                value: 'typography',
                                label: 'Typography 字体'
                            }, {
                                value: 'icon',
                                label: 'Icon 图标'
                            }, {
                                value: 'button',
                                label: 'Button 按钮'
                            }]
                        }, {
                            value: 'form',
                            label: 'Form',
                            children: [{
                                value: 'radio',
                                label: 'Radio 单选框'
                            }, {
                                value: 'checkbox',
                                label: 'Checkbox 多选框'
                            }, {
                                value: 'input',
                                label: 'Input 输入框'
                            }, {
                                value: 'input-number',
                                label: 'InputNumber 计数器'
                            }, {
                                value: 'select',
                                label: 'Select 选择器'
                            }, {
                                value: 'cascader',
                                label: 'Cascader 级联选择器'
                            }, {
                                value: 'switch',
                                label: 'Switch 开关'
                            }, {
                                value: 'slider',
                                label: 'Slider 滑块'
                            }, {
                                value: 'time-picker',
                                label: 'TimePicker 时间选择器'
                            }, {
                                value: 'date-picker',
                                label: 'DatePicker 日期选择器'
                            }, {
                                value: 'datetime-picker',
                                label: 'DateTimePicker 日期时间选择器'
                            }, {
                                value: 'upload',
                                label: 'Upload 上传'
                            }, {
                                value: 'rate',
                                label: 'Rate 评分'
                            }, {
                                value: 'form',
                                label: 'Form 表单'
                            }]
                        }, {
                            value: 'data',
                            label: 'Data',
                            children: [{
                                value: 'table',
                                label: 'Table 表格'
                            }, {
                                value: 'tag',
                                label: 'Tag 标签'
                            }, {
                                value: 'progress',
                                label: 'Progress 进度条'
                            }, {
                                value: 'tree',
                                label: 'Tree 树形控件'
                            }, {
                                value: 'pagination',
                                label: 'Pagination 分页'
                            }, {
                                value: 'badge',
                                label: 'Badge 标记'
                            }]
                        }, {
                            value: 'notice',
                            label: 'Notice',
                            children: [{
                                value: 'alert',
                                label: 'Alert 警告'
                            }, {
                                value: 'loading',
                                label: 'Loading 加载'
                            }, {
                                value: 'message',
                                label: 'Message 消息提示'
                            }, {
                                value: 'message-box',
                                label: 'MessageBox 弹框'
                            }, {
                                value: 'notification',
                                label: 'Notification 通知'
                            }]
                        }, {
                            value: 'navigation',
                            label: 'Navigation',
                            children: [{
                                value: 'menu',
                                label: 'NavMenu 导航菜单'
                            }, {
                                value: 'tabs',
                                label: 'Tabs 标签页'
                            }, {
                                value: 'breadcrumb',
                                label: 'Breadcrumb 面包屑'
                            }, {
                                value: 'dropdown',
                                label: 'Dropdown 下拉菜单'
                            }, {
                                value: 'steps',
                                label: 'Steps 步骤条'
                            }]
                        }, {
                            value: 'others',
                            label: 'Others',
                            children: [{
                                value: 'dialog',
                                label: 'Dialog 对话框'
                            }, {
                                value: 'tooltip',
                                label: 'Tooltip 文字提示'
                            }, {
                                value: 'popover',
                                label: 'Popover 弹出框'
                            }, {
                                value: 'card',
                                label: 'Card 卡片'
                            }, {
                                value: 'carousel',
                                label: 'Carousel 走马灯'
                            }, {
                                value: 'collapse',
                                label: 'Collapse 折叠面板'
                            }]
                        }]
                    }, {
                        value: 'ziyuan',
                        label: '资源',
                        children: [{
                            value: 'axure',
                            label: 'Axure Components'
                        }, {
                            value: 'sketch',
                            label: 'Sketch Templates'
                        }, {
                            value: 'jiaohu',
                            label: '组件交互文档'
                        }]
                    }],
                cascaderProps: {
                    expandTrigger: 'hover',
                    // 指定选中的值
                    value: 'value',
                    // 显示的值
                    label: 'label',
                    children: 'children'
                },
                previeDialogVisible: false,
                previewImgPath: '',
            }
        },
        created() {
            this.initPage();
        },
        methods: {
            initPage() {

            },
            handleChange(value) {
                if (this.baseInfo.sort.length !== 3) {
                    this.baseInfo.sort = [];
                }
                console.log(value);
            },
            // 切换标签之前的钩子，若返回 false 或者返回 Promise 且被 reject，则阻止切换。
            beforeTabLeave(active, old) {
                if (active !== 'baseInfo') {
                    if (this.baseInfo.sort.length !== 3) {
                        this.$message.warning("当前商品分类未选择");
                        return false;
                    } else {
                        this.activeIndex = this.tabs.indexOf(active);
                    }
                }
            },
            tabClick(tab) {
                console.log(tab.name);
            },
            // 移除图片
            handleRemove(file, fileList) {
                // TODO this.baseInfo.pics
                console.log(file, fileList);
            },
            // 图片预览
            handlePreview(file) {
                console.log(file);
                this.previeDialogVisible = true;
                this.previewImgPath = file.url;
            },
            // 处理上传图片
            handleSuccess(response, file, fileList) {
                // TODO this.baseInfo.pics

            },
            createGoods(){
                this.$message.success('创建成功!');
                this.$router.push('/goodsList')
            }

        }
    }
</script>

<style lang="less" scoped>
    .el-checkbox {
        margin: 0 10px 0 0 !important;
    }

    .previewImg{
        width: 100%;
    }

    .btnAdd{
        margin-top: 15px;
    }

</style>
